import sys
import time

from PySide2.QtWidgets import QApplication, QWidget
from PySide2 import QtCore
from PySide2.QtCore import *


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(300, 300, 300, 200)
        self.setFixedWidth(300)
        self.setFixedHeight(200)
        self.setWindowTitle('按键检测')
        self.c_time = None
        self.input_keys = ""
        self.show()

    # 检测键盘回车按键
    def keyPressEvent(self, event):
        c_time = time.time()
        print(f"event.key():{event.key()},event.text():{event.text()}")
        if event.key() == 16777220:
            # self.c_time = c_time
            # 开始时间记录
            if self.input_keys:
                print(self.input_keys)
                # self._torquegun_data["qrcode"] = self.input_keys
                # threading.Thread(target=self.mes_request, args=(self.input_keys,)).start()
                # self.mes_request(self.input_keys)
                self.input_keys = ""
            return

        if not self.c_time:
            self.c_time = c_time
            # return

        if c_time - self.c_time >= 10:
            self.c_time = c_time
            return
        self.input_keys += event.text()
        # self.input_keys += event.text()
        self.c_time = c_time
        # if (48 <= key <= 58) or (65 <= key <= 126):
        # 扫码构建

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            print("鼠标左键点击")
        elif event.button() == Qt.RightButton:
            print("鼠标右键点击")
        elif event.button() == Qt.MidButton:
            print("鼠标中键点击")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    sys.exit(app.exec_())
